package org.example.algorithm.dp;

public class LongestCommonSubStringSolution {

    public static void main(String[] args) {
        String text1 = "abcbcde";
        String text2 = "bbcbce";
        LongestCommonSubStringSolution solution = new LongestCommonSubStringSolution();
        int res = solution.getLongestCommonSubString(text1, text2);
        System.out.println(res);
    }

    public int getLongestCommonSubString(String str1, String str2) {
        int[][] dp = new int[str1.length()][str2.length()];
        int max = 0;
        for (int i=0;i<str1.length();i++) {
            for (int j=0;j<str2.length();j++) {
                if (str1.charAt(i) == str2.charAt(j)) {
                    if (i>=1 && j>=1) {
                        dp[i][j] = dp[i-1][j-1] + 1;
                    } else {
                        dp[i][j] = 1;
                    }
                } else {
                    dp[i][j] = 0;
                }
                max = Math.max(max, dp[i][j]);
            }
        }
        return max;
    }
}
